<template>
  <div>	 
  	<header class="classification_t header_fixed" id="title">
      <div class="classification_t_l" @click="goPrev">
            <img src="./../../assets/images/return.png" width="30%">
      </div>
      <div class="classification_t_r">{{$route.meta.title}}</div>
    </header>
    <div class="shipping_adr_con_t" style="margin-top:50px;">
      <div class="shipping_adr_con_t_l">
        <div class="shipping_adr_con_t_l_l">
            <img src="@/assets/images/location_icon_1.png">
        </div>
        <div class="shipping_adr_con_t_l_r">我的收货地址</div>
      </div>
      <div class="shipping_adr_con_t_r" @click="Openaddadres">
        <div><span>+</span><span>新加收货地址</span></div>
      </div>
    </div>
    <div v-for="(item,index) of adresses">
      <div class="shipping_adr_name">
        <div class="shipping_adr_name_t" @click="choseAdress(item)"  v-if=" goto == 1 ">
          <p>{{item.linkman}}<span>{{item.phone}}</span></p>
          <div>{{item.address}}</div>
        </div>
        <div class="shipping_adr_name_t" v-else>
          <p>{{item.linkman}}<span>{{item.phone}}</span></p>
          <div>{{item.address}}</div>
        </div>
        <div class="shipping_adr_name_b">
          <div class="shipping_adr_name_b_l">
            <input class="aui-radio" type="radio" name="demo1" v-model="item.is_default" value="1" @click="setDefault(item.id)" > 默认地址
          </div>
          <div class="shipping_adr_name_b_r">
            <span><img src="@/assets/images/update_green.png" @click="updateAdress(item)"></span>
            <span><img src="@/assets/images/delete_red.png" @click="Delateadress(item.id,index)"></span>
          </div>
        </div>
      </div>
      <div class="devivid"></div>
    </div>
  </div>
</template>
<script>
// import TopWithBack from './../common/TopWithBack'
import { loadAdress, setDefaultAdress, delAdress } from '@/api/allapis'
import { Message, MessageBox, Loading } from 'element-ui'
export default{
  name: 'Adresses',
  data(){
    return{
        adresses:'',
        goto:''//接受传过来的goto参数，goto为1时，页面是从预约页面跳转过来的，点击地址执行choseAdress()函数
    }
  },
  // components:{
  //   TopWithBack
  // },
  methods:{
    //拉取地址列表
    loadAdress(){
        loadAdress().then(response=>{
          // console.log(JSON.stringify(response))
            this.adresses = response.data.list
        })
    },
    //打开添加地址页面
    Openaddadres(){
      this.$router.push({
        path:'/addadress',
        query:{
          goto:this.goto
        }
      })
    },
    //设置默认地址
    setDefault(id){
      setDefaultAdress(id).then(response=>{
        this.$vux.toast.show({
          text: response.msg,
          type: 'success',
          time: 800
        })
        this.loadAdress()
      })
    },
    //打开修改地址页面
    updateAdress(item){
      this.$router.push({
        name:'UpdateAdress',
        params:{
          adressitem:item//传给修改地址页面的地址信息
        }
      })
    },
    //选择地址
    choseAdress(item){
        this.setDefault(item.id)
        this.$router.go(-1)
    },
    //删除地址
    Delateadress(id,index){
        const self = this
        this.$vux.confirm.show({
          title: '删除地址',
          content: '确定删除此地址吗？',
          onConfirm () {
            delAdress(id).then(response=>{
            self.adresses.splice(index,1)
            self.loadAdress()
          })
        }
      })
    },
    //返回上一页
    goPrev(){
      this.$router.go(-1)
    },
  },
  created(){
    this.goto = this.$route.query.goto//接受传过来的goto参数，goto为1时，页面是从预约页面跳转过来的
    this.loadAdress()
  },
}
</script>
<style>

</style>


